package com.qiu.location.app.ui.main;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.fallenpanda.location.bean.MyLocation;
import com.fallenpanda.location.manager.MainLocationListener;
import com.fallenpanda.location.manager.MainLocationManager;
import com.qiu.location.app.R;
import com.qiu.location.app.ui.BaseActivity;

/**
 * 主界面
 */
public class MyActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "MyActivity";

    private long exitTime = 0;

    private TextView mTvText;
    private CheckBox mCbxNeedAddr;
    private EditText mEdtScanSpan;

    private MainLocationManager mainLocationManager;

    @Override
    protected void onResume() {
        super.onResume();
        mainLocationManager.registerListener(mainLocationListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mainLocationManager.unregisterListener(mainLocationListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        mainLocationManager = MainLocationManager.getInstance();

        initView();
    }

    private void initView(){
        mTvText = (TextView) findViewById(R.id.textview);
        mCbxNeedAddr = (CheckBox) findViewById(R.id.isAddrInfocb);
        mEdtScanSpan = (EditText) findViewById(R.id.edit1);

        findViewById(R.id.btnOnce).setOnClickListener(this);
        findViewById(R.id.btnAuto).setOnClickListener(this);
        findViewById(R.id.btnStop).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnOnce:
                boolean isNeedAress = mCbxNeedAddr.isChecked();
                mainLocationManager.getBaiduLocationOnce(isNeedAress);
                break;
            case R.id.btnAuto:
                String scanSpan = mEdtScanSpan.getText().toString().trim();
                mainLocationManager.getBaiduLocationAuto(Integer.valueOf(scanSpan));
                break;
            case R.id.btnStop:
                mainLocationManager.stopBaiduLocation();
                break;
        }
    }

    private MainLocationListener mainLocationListener = new MainLocationListener() {
        @Override
        public void onLocationChanged(MyLocation location) {
            if (location == null)
                return ;
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getmTime());
            sb.append("\nlatitude : ");
            sb.append(location.getmLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getmLongitude());
            if(location.ismHasAccuracy()){
                sb.append("\nradius : ");
                sb.append(location.getmAccuracy());
            }
            if(location.ismHasSpeed()){
                sb.append("\nspeed : ");
                sb.append(location.getmSpeed());
            }
            if(location.ismHasAddress()){
                sb.append("\n省：");
				sb.append(location.getmProvince());
				sb.append("\n市：");
				sb.append(location.getmCity());
				sb.append("\n区/县：");
				sb.append(location.getmDistrict());
                sb.append("\naddr : ");
                sb.append(location.getmAddress());
            }
            mTvText.setText(sb);
        }

        @Override
        public void onLocationFail(String provider) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    /**
     * 监听返回--是否退出程序
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean flag = true;
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(this,"再按一次退出",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }else{
                context.exit();
            }
        }else{
            flag = super.onKeyDown(keyCode, event);
        }
        return flag;
    }


}
